<?php
/*
 * 企业类
 * @arrange Mark
 * @package ixdcw
 * @file    company.class.php
 * @time    12-12-19 下午1:01
 */
defined('IN_IXDCW') or exit('Access Denied');
class Company extends BasicModule {
	private $_userid;
	private $_username;
	private $_table_member;
	private $_table_company;
	private $_table_company_catid;
	private $_table_company_data;
	function __construct($username = '') {
		parent::__construct();
		$this->_table_member        = $this->_db->pre . 'member';
		$this->_table_company       = $this->_db->pre . 'company';
		$this->_table_company_catid = $this->_db->pre . 'company_catid';
		$this->_table_company_data  = $this->_db->pre . 'company_data';
	}
	public function setUserid($userid) {
		$this->_userid = $userid;
	}

	public function getUserid() {
		return $this->_userid;
	}
	function getOne($username = '') {
		$sql = $username ? "m.username='$username'" : "m.userid='$this->_userid'";
		return $this->_db->getOne("SELECT * FROM {$this->_table_member} m,{$this->_table_company} c WHERE m.userid=c.userid AND $sql");
	}

	function getList($condition, $order = 'userid DESC', $cache = '') {
		global $items,$kw,$replacef,$replacet;
		$num = $this->_db->count($this->_table_company, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table_company}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$members = array();
		$result  = $this->_db->query($sql, $cache);
		if($kw) {
			$replacef = explode(' ', $kw);
			$replacet = array_map('highlight', $replacef);
		}
		while ($r = $this->_db->fetchArray($result)) {
			if($kw) $r['company'] = str_replace($replacef, $replacet, $r['company']);
			$members[] = $r;
		}
		return $members;
	}

	function update($userid) {
		global $DT, $MOD, $CATEGORY;
		$CATEGORY or $CATEGORY = PCache::read('category-4.php');
		$this->_userid = $userid;
		$r            = $this->getOne();
		if (!$r) return false;
		$keyword = addslashes($r['company'] . strip_tags(area_pos($r['areaid'], '')) . $r['business'] . $r['sell'] . $r['buy'] . $r['mode']);
		$catids  = '';
		if ($r['catid']) {
			$catids = explode(',', substr($r['catid'], 1, -1));
			$cids   = '';
			foreach ($catids as $catid) {
				if (isset($CATEGORY[$catid])) {
					$catid = $CATEGORY[$catid]['parentid'] ? $CATEGORY[$catid]['arrparentid'] . ',' . $catid : $catid;
					$cids .= $catid . ',';
				}
			}
			$cids        = array_unique(explode(',', substr(str_replace(',0,', ',', ',' . $cids), 1, -1)));
			$catids      = ',' . implode(',', $cids) . ',';
			$r['catids'] = $catids;
		}
		if ($r['vip']) {
			$vipt     = $this->getVipt($r['username']);
			$vip      = $this->getVip($vipt, $r['vipr']);
			$r['vip'] = $vip;
			$this->_db->query(Sql::update($this->_table_company,",keyword='$keyword',catids='$catids',vip='$vip',vipt='$vipt'","userid=$userid"));
		} else {
			$this->_db->query(Sql::update($this->_table_company,",keyword='$keyword',catids='$catids'","userid=$userid"));
		}
		$this->updateCatid($r);
		return true;
	}

	function getVipt($username) {
		global $MOD, $GROUP, $DT_TIME;
		$GROUP or $GROUP = PCache::read('group.php');
		$r         = $this->getOne($username);
		$_groupvip = $GROUP[$r['groupid']]['vip'] > $MOD['vip_maxgroupvip'] ? $MOD['vip_maxgroupvip'] : $GROUP[$r['groupid']]['vip'];
		$_cominfo  = $r['validated'] ? intval($MOD['vip_cominfo']) : 0;
		$_year     = $r['fromtime'] ? (date('Y', $DT_TIME) - date('Y', $r['fromtime'])) * $MOD['vip_year'] : 0;
		$_year     = $_year > $MOD['vip_maxyear'] ? $MOD['vip_maxyear'] : $_year;
		$total     = intval($_groupvip + $_cominfo + $_year);
		if ($total > 10) $total = 10;
		if ($total < 1) $total = 1;
		return $total;
	}

	function getVip($vipt, $vipr) {
		$vip = intval($vipt + ($vipr));
		if ($vip > 10) $vip = 10;
		if ($vip < 1) $vip = 1;
		return $vip;
	}

	function vipEdit($vip) {
		global $_username, $DT_TIME;
		if (!is_array($vip)) return false;
		if (!$vip['username']) return $this->err(lang('message->pass_company_username'));
		$r = $this->getOne($vip['username']);
		if (!$r) return $this->err(lang('message->pass_company_notuser'));
		$userid = $r['userid'];
		if ($r['groupid'] < 5) return $this->err(lang('message->pass_company_badgroup'));
		if (!$vip['groupid']) return $this->err(lang('message->pass_company_group'));
		if (!$vip['fromtime'] || !is_date($vip['fromtime'])) return $this->err(lang('message->pass_company_fromdate'));
		if (!$vip['totime'] || !is_date($vip['totime'])) return $this->err(lang('message->pass_company_todate'));
		if (strtotime($vip['fromtime'] . ' 00:00:00') > strtotime($vip['totime'] . ' 23:59:59')) return $this->err(lang('message->pass_company_baddate'));
		$vip['fromtime']  = strtotime($vip['fromtime'] . ' 00:00:00');
		$vip['totime']    = strtotime($vip['totime'] . ' 23:59:59');
		$vip['validated'] = $vip['validated'] ? 1 : 0;
		$vip['validtime'] = strtotime($vip['validtime']);
		$vip['vipr']      = isset($vip['vipr']) ? $vip['vipr'] : 0;
		$this->_db->query(Sql::update($this->_table_company,"groupid='$vip[groupid]',validated='$vip[validated]',validator='$vip[validator]',validtime='$vip[validtime]',vipr='$vip[vipr]',fromtime='$vip[fromtime]',totime='$vip[totime]'","userid=$userid"));
		$vip['vipt'] = $this->getVipt($vip['username']);
		$vip['vip']  = $this->getVip($vip['vipt'], $vip['vipr']);
		$this->_db->query(Sql::update($this->_table_company,"vip='$vip[vip]',vipt='$vip[vipt]'","userid=$userid"));
		$this->_db->query(Sql::update($this->_table_company_catid,"vip='$vip[vip]'","userid=$userid"));
		$this->_db->query(Sql::update($this->_table_member,"groupid='$vip[groupid]'","userid=$userid"));
		return true;
	}

	function vipDelete($userid) {
		if (!isset($userid) || !$userid) return false;
		$userids = is_array($userid) ? implode(',', $userid) : intval($userid);
		$result  = $this->_db->query("SELECT * FROM {$this->_table_member} WHERE userid IN ($userids)");
		while ($r = $this->_db->fetchArray($result)) {
			$regid = $r['regid'] ? $r['regid'] : 6;
			$this->_db->query(Sql::update($this->_table_company,"groupid=$regid,vip=0,vipr=0,vipt=0","userid=$r[userid]"));
			$this->_db->query(Sql::update($this->_table_company_catid,"groupid=$regid,vip=0","userid=$r[userid]"));
			$this->_db->query(Sql::update($this->_table_member,"groupid=$regid,regid=$regid","userid=$r[userid]"));
		}
		return true;
	}

	function level($userid, $level) {
		$userids = is_array($userid) ? implode(',', $userid) : $userid;
		$this->_db->query(Sql::update($this->_table_company,"level=$level","userid IN ($userids)"));
	}

	function updateCatid($member = array()) {
		if ($member) {
			$userid  = $member['userid'];
			$groupid = intval($member['groupid']);
			$vip     = intval($member['vip']);
			$areaid  = intval($member['areaid']);
			$catids  = $member['catid'];
			$this->_db->query(Sql::delete($this->_table_company_catid,"userid=$userid"));
			if ($catids) {
				foreach (explode(',', $catids) as $catid) {
					$catid = intval($catid);
					if (!$catid) continue;
					$this->_db->query("INSERT INTO {$this->_table_company_catid} (userid,catid,areaid,groupid,vip) VALUES ($userid,$catid,$areaid,$groupid,$vip)");
				}
			}
		}
	}
}